//
//  CHProgressBar.swift
//  bridging_js
//
//  Created by cyptt on 2021/9/10.
//

import UIKit
let CHSimple_ScreenHeight_CA = UIScreen.main.bounds.size.height
let CHSimple_ScreenWidth_CA = UIScreen.main.bounds.size.width
public class CHProgressBar: UIView,UIGestureRecognizerDelegate {

    //背景区域的颜色和透明度
    var backgroundColor1:UIColor  = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.4)
    //进度条view
    var progressView:UIView = UIView()
    //提示按钮
    var hintBtn:UIButton!
    var beforeValue:CGFloat = 0 //前一个值
    var displayLink: CADisplayLink! //定时器 承接控制器里的定时器，删除view时保证定时器关闭
    var path:UIBezierPath!
    var progressLayer:CAShapeLayer!
    var deaultColor:UIColor?
    var trackColor:UIColor?
    var percentColor:UIColor?
    
    
    
    
    //初始化视图
   public func initView(deaultColor:UIColor,trackColor:UIColor,percentColor:UIColor) -> CHProgressBar {
        self.frame = CGRect.init(x: 0, y: 0, width:CHSimple_ScreenWidth_CA , height: CHSimple_ScreenHeight_CA)
    
        self.deaultColor = deaultColor
        self.trackColor = trackColor
        self.percentColor = percentColor
        
        self.backgroundColor = backgroundColor1
//        let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapBtnAndcancelBtnClick))
//        tap.delegate = self
//
//
//        self.addGestureRecognizer(tap)
        return self
    }
    
 
    //弹出view
    public func addAnimate(view:CHProgressBar)  {
        self.addProgressView()
        UIApplication.shared.keyWindow?.addSubview(view)
    }
    
    //添加进度条
    func addProgressView()  {
        progressView = UIView.init(frame: CGRect.init(x: 25, y: 290, width: CHSimple_ScreenWidth_CA - 50, height: 15))
        progressView.layer.masksToBounds = true
        progressView.layer.cornerRadius = 7.5
        progressView.backgroundColor =  self.deaultColor
        self.addSubview(progressView)
        hintBtn = UIButton.init(type: UIButton.ButtonType.custom)
        hintBtn.frame = CGRect.init(x: 8, y: progressView.frame.minY - 30, width: 34, height: 20)
        hintBtn.setTitle("0.0%", for: UIControl.State.normal)
        hintBtn.titleLabel?.font = UIFont.systemFont(ofSize: 8)
        hintBtn.setTitleColor(self.percentColor!, for: UIControl.State.normal)
        self.addSubview(hintBtn)
        self.gradentWith(frame: progressView.frame)
        
    }
    
    //为进度条添加遮罩，及layer
    @objc func gradentWith(frame:CGRect){
        path = UIBezierPath.init()
        path.stroke() //添加zhe zhao
        progressLayer = CAShapeLayer.init()
        progressLayer.frame = progressView.bounds
        progressLayer.strokeColor = UIColor.red.cgColor  //warn
        progressLayer.lineCap = CAShapeLayerLineCap.init(rawValue: "kCALineCapRound")
        progressLayer.lineWidth = progressView.frame.size.height //渐变图层
        let grain:CALayer = CALayer.init()
        let gradientLayer:CAGradientLayer = CAGradientLayer.init()
        let fixColor:UIColor = self.trackColor!
        let preColor:UIColor = self.trackColor!
        gradientLayer.frame = CGRect.init(x: 0, y: 0, width: progressView.frame.size.width, height: progressView.frame.size.height)
        gradientLayer.colors = [preColor.cgColor,fixColor.cgColor]
        // 开始点
        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
        // 结束点
        gradientLayer.endPoint = CGPoint.init(x: 1, y: 1)
        grain.addSublayer(gradientLayer)
        grain.mask = progressLayer
        progressView.layer.addSublayer(grain)//增加动画
        let pathAnimation : CABasicAnimation = CABasicAnimation.init(keyPath: "strokeEnd")
        pathAnimation.duration = 0;
        pathAnimation.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName(rawValue: "linear"))
        pathAnimation.fromValue = NSNumber.init(value: 0.0)
        pathAnimation.toValue = NSNumber.init(value: 1.0)
        pathAnimation.autoreverses = false
        pathAnimation.repeatCount = 1
        progressLayer.add(pathAnimation, forKey: "strokeEndAnimation")
        
    }
    
    //当前进度
   public func passValue(currentValue:CGFloat,allValue:CGFloat)  {
        if currentValue < allValue {
            // 当前比例
            let currentProportion : CGFloat = currentValue/allValue
    
            hintBtn.frame = CGRect.init(x: 8 + currentProportion * progressView.frame.size.width, y: progressView.frame.minY - 30, width: 34, height: 20)
            hintBtn.setTitle("\(NSInteger(currentProportion*100))%", for: UIControl.State.normal)
            path.move(to: CGPoint.init(x: progressView.frame.size.width * (beforeValue/allValue), y: progressView.frame.size.height/2))
            path.addLine(to: CGPoint.init(x: progressView.frame.size.width * currentProportion, y: progressView.frame.size.height/2))
            progressLayer.path = path.cgPath
        }
        else{
            //上传/下载成功 隐藏当前状态
            self.tapBtnAndcancelBtnClick()
        }
        beforeValue = currentValue
    }
    
    
    
    @objc func tapBtnAndcancelBtnClick(){
        self.removeFromSuperview()
        self.progressView.removeFromSuperview()
        if displayLink != nil {
            displayLink.invalidate()
            displayLink = nil
        }
    }
    
    public func stop(){
        if displayLink != nil {
            displayLink.invalidate()
            displayLink = nil
        }

    }
   
  
}
